package spec.zyh;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

@WebServlet(name = "DeleteServlet")
public class DeleteServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out1 = response.getWriter();
        PreparedStatement Pstmt=null;
        try {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
//            String t = request.getParameter("username");
//            String c = request.getParameter("password");
//            String urL="jdbc:mysql://Localhost:3306/1613580003?characterEncoding=utf8";
//            String name="root";
//            String password="123456";
//            Class.forName("com.mysql.jdbc.Driver");
//            Connection com= null;
//            com = DriverManager.getConnection(urL,name,password);
//            Statement s=com.createStatement();
            DatabaseConnetion dbc=new DatabaseConnetion();
            Connection com=dbc.getConnection();
            Statement s = null;
            s = com.createStatement();
            String id=request.getParameter("id");
            ResultSet rs=s.executeQuery("select * from t_news where id="+id);
            if(rs.next()){
                String sql = "delete from t_news where id="+id;
                Pstmt =com.prepareStatement(sql);
                int result=Pstmt.executeUpdate();
                if(result==1){
                    response.sendRedirect("delete.html");
                }
                else {
                    out1.println("删除失败");
                }
            }
            dbc.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
